//
//  CTVChannel.m
//  Channltv
//
//  Created by Bruno Keymolen on 7/5/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVChannel.h"
#import "CTVCache.h"

@implementation CTVChannel


-(id)initWithRootChannelPointer:(id)pRootChannel{
	[super init];

	m_p_rootChannel = pRootChannel; //no reference counting on this pointer (!) //DO REFERENCE COUNTING -> @TODO:CHECK
	m_clips = [[NSMutableArray alloc] init];
	m_watchPoint = [[CTVChannelWatchPoint alloc] init];	
	m_player = [[BRQTKitVideoPlayer alloc] init];
	
	return self;
 }


-(void) dealloc{ 
	int i;
	
	//First free all programs
	for(i=0;i<[m_clips count];i++){
		[[m_clips objectAtIndex:i] release];		
	}
	
	if(m_thumbTexture != nil){
		[m_thumbTexture release];
	}

	[m_clips removeAllObjects];		
	[m_clips release];
	[m_watchPoint release];
	[m_player release];
	
	[super dealloc]; //do not forget to transmit to the superclass 
} 


-(int)removeAllPrograms{
	int i;
	
	//First free all programs
	for(i=0;i<[m_clips count];i++){
		[[m_clips objectAtIndex:i] release];		
	}
	
	[m_clips removeAllObjects];		
	
	return 0;
}

-(CTVClip*)createNewProgram{
	CTVClip* prog = [[CTVClip alloc]init];
	[m_clips addObject:prog];
	return prog;
}

-(void)addProgram:(CTVClip*)prog{
	//Insert new clips at the top, so the newest ar shown first on tv
	//[m_clips insertObject:prog atIndex:0]; (the server sends the right order now)
	[m_clips addObject:prog];	
}

-(int)count{
	return [m_clips count];
}

-(CTVClip*)GetProgramAtIndex:(int)index{
	return [m_clips objectAtIndex:index];
}


-(int)RemoveProgramAtIndex:(int)index{
	CTVClip* prog = [self GetProgramAtIndex:index];
	return [self RemoveProgram:prog];
}


-(int)RemoveProgram:(CTVClip*)prog{
	[m_clips removeObject:prog];
	[prog release];
	return 0;
}


-(void)setThumbUrl:(NSString*)thumbUrl{
	[thumbUrl retain];
	[m_thumbUrl release];
	m_thumbUrl = thumbUrl;
}


-(NSString*)thumbUrl{
	return m_thumbUrl;
}


-(void)setUrl:(NSString*) url{
	if (m_url != nil) {
		[m_url dealloc];	
	}
	m_url =  [[NSString alloc] initWithString:url];  
}


-(NSString*)url{
	return m_url;
}


-(void)setName:(NSString*)name{
	[name retain];
	[m_name release];
	m_name = name;
}

-(NSString*)name{
	return m_name;
}

-(void)setChannelId:(NSString*)channelID{
	[channelID retain];
	[m_channelID release];
	m_channelID = channelID;
}

-(NSString*)channelID{
	return m_channelID;
}


-(void)setWillBeDeleted:(BOOL)flag{
	m_willBeDeleted = flag;
}

-(BOOL)willBeDeleted{
	return m_willBeDeleted;
}

-(NSArray*)clips{
	return m_clips;
}

-(void)setRootChannelPointer:(id)pRootChannel{
	m_p_rootChannel = pRootChannel;
}

-(id)pRootChannel{
	return m_p_rootChannel;
}

-(BRBitmapTexture*)thumbTextureForScene:(BRRenderScene*)scene{
	NSString* cachePath;
	if(m_thumbTexture == nil){
		if([m_thumbUrl hasPrefix:@"http"]){
			cachePath = [CTVCache getResourceCachePathForURL:m_thumbUrl];
		}else{
			cachePath = m_thumbUrl;
		}
		m_thumbTexture = [CTVCache createBitmapTextureFromPath:cachePath withScene:scene];
		[m_thumbTexture retain];
	}
	return m_thumbTexture;
}

//
//Watchpoint stuff
//
-(void)resetWatchPoint{
NSLog(@"resetWatchPoint");
	[m_watchPoint setClipIndex:[m_clips count]-1]; //0 based ??
	CTVClip* thisClip = [m_clips objectAtIndex:[m_watchPoint clipIndex]];
	[m_watchPoint setClipID:[thisClip clipID]];
	[m_watchPoint setInitialized:TRUE];
	//Cache this clip
	if(thisClip != nil){
		[[CTVCache GetInstance] cacheMedia:[thisClip getUrl] andClip:thisClip];
	}
}

-(void)updateWatchPointWithClip:(CTVClip*)clip{
NSLog(@"updateWatchPointWithClip:%@",[clip clipID] );
	if([m_clips indexOfObject:clip] >= 0){
		[m_watchPoint setClipIndex:[m_clips indexOfObject:clip]-1]; //0 based??
	}
	[m_watchPoint setClipID:[clip clipID]];	
	//Cache this clip
	if(clip != nil){
		[[CTVCache GetInstance] cacheMedia:[clip getUrl] andClip:clip];
	}
}


-(CTVChannelWatchPoint*)watchPoint{
	return m_watchPoint;
}

-(CTVClip*)nextClipToPlay{
	CTVClip* newerClip = nil;
	CTVClip* thisClip = nil;
	//Try first with ID
	NSEnumerator* en = [m_clips objectEnumerator];
	while(thisClip = [en nextObject]){
		if( [[thisClip clipID] isEqualToString:[m_watchPoint clipID]] ){
			thisClip = newerClip; //take the previous one (newer one) 
			break;
		}
		newerClip = thisClip;
	}
	//If ID is not found any more, use position
	if(thisClip == nil){
		if([m_watchPoint clipIndex]-1 <= [m_clips count] && [m_watchPoint clipIndex]-1 >= 0){
			thisClip = [m_clips objectAtIndex:[m_watchPoint clipIndex]-1];
		}
	}
	
	//Update watchpoint
	if(thisClip != nil){
		[m_watchPoint setClipID:[thisClip clipID]];
		[m_watchPoint setClipIndex:[m_clips indexOfObject:thisClip]-1];
	}
	
	return thisClip;
}

-(CTVClip*)prevClipToPlay{
	CTVClip* thisClip;
	//Try first with ID
	NSEnumerator* en = [m_clips objectEnumerator];
	while(thisClip = [en nextObject]){
		if( [[thisClip clipID] isEqualToString:[m_watchPoint clipID]] ){
			thisClip = [en nextObject]; //take the next one 
			break;
		}
	}
	//If ID is not found any more, use position
	if(thisClip == nil){
		if([m_watchPoint clipIndex]+1 <= [m_clips count]){
			thisClip = [m_clips objectAtIndex:[m_watchPoint clipIndex]+1];
		}
	}
	
	//Update watchpoint
	if(thisClip != nil){
		[m_watchPoint setClipID:[thisClip clipID]];
		[m_watchPoint setClipIndex:[m_clips indexOfObject:thisClip]];
	}
	
	return thisClip;
}


-(CTVClip*)currentClipToPlay{
	CTVClip* thisClip;
	//Try first with ID
	NSEnumerator* en = [m_clips objectEnumerator];
	while(thisClip = [en nextObject]){
		if( [[thisClip clipID] isEqualToString:[m_watchPoint clipID]] ){
NSLog(@"currentClipToPlay, found :");		
			break;
		}
	}
	//If ID is not found any more, use position
	if(thisClip == nil){
		if([m_watchPoint clipIndex] <= [m_clips count]){
			thisClip = [m_clips objectAtIndex:[m_watchPoint clipIndex]];
NSLog(@"currentClipToPlay, not found :");		
		}
	}
	
NSLog(@"currentClipToPlay !!!!");
	
	return thisClip;	
}


-(BRQTKitVideoPlayer*)playerForMediaURLString:(NSString*)urlString{
	if(m_playerMediaURLString != nil){
		if( ![m_playerMediaURLString isEqualToString:urlString] ){
			NSLog(@"ResetPlayer");
			[m_player setMedia:nil error:nil];
			[m_player resetPlayer];
		} 
		[m_playerMediaURLString release];
	}
	m_playerMediaURLString = urlString;
	[m_playerMediaURLString retain];
	
	return m_player;
}

@end


/********************************************
 * Watchpoint
 ***************************/
@implementation CTVChannelWatchPoint

-(id)init{
	[super init];
	
	return self;
}

-(void)dealloc{
	if(m_clipID)[m_clipID release];
	[super dealloc];
}

-(void)setInitialized:(BOOL)val{
	m_initialized = val;
}

-(void)setClipID:(NSString*)clipID{
	if(m_clipID)[m_clipID release];
NSLog(@"CTVChannel Watchpoint, setClipID:%@", clipID );	
	m_clipID = clipID;
	[m_clipID retain];
}

-(void)setClipIndex:(int)clipIndex{
NSLog(@"CTVChannel Watchpoint, setClipIndex:%d", clipIndex );	
	m_clipIndex = clipIndex;
}

-(void)setOffset:(float)offset{
NSLog(@"setOffset:%f", offset);
	m_offset = offset;
}

-(NSString*)clipID{
NSLog(@"CTVChannel Watchpoint, clipID:%@", m_clipID );	
	return m_clipID;
}

-(int)clipIndex{
NSLog(@"CTVChannel Watchpoint, clipIndex:%d", m_clipIndex );	

	return m_clipIndex;
}

-(float)offset{
	return m_offset;
}

-(BOOL)initialized{
	return m_initialized;
}


@end

